from moviepy.editor import VideoFileClip
import os




# 设置每个切割视频的时长（单位：秒）
duration = 5 * 60

# 遍历指定文件夹下的所有视频文件
folder_path = "./"
for file_name in os.listdir(folder_path):
    if file_name.endswith(".mp4"):
        video_path = os.path.join(folder_path, file_name)
        video_clip = VideoFileClip(video_path)

        # 计算视频总时长
        total_duration = video_clip.duration

        # 切割视频
        start_time = 0
        end_time = duration
        clip_num = 1
        while start_time < total_duration:
            if end_time > total_duration:
                end_time = total_duration
            subclip = video_clip.subclip(start_time, end_time)
            subclip.write_videofile(f"{file_name}_part{clip_num}.mp4")
            start_time += duration
            end_time += duration
            clip_num += 1

        video_clip.close()
        print(f"{file_name}已裁剪")
